package dao;

import java.io.File;
import java.util.List;
import java.util.Map;

import com.sleepycat.bind.EntryBinding;
import com.sleepycat.bind.serial.SerialBinding;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.bind.tuple.StringBinding;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Transaction;

public abstract class CommonDAO {
	
	protected Environment env;
	protected String envDir;
	protected Database db;
	protected EntryBinding<String> keyBinding;
	protected StoredClassCatalog catalog;
	Transaction txn = null;
	
	public CommonDAO(String envDir, String dbName){
		EnvironmentConfig envConfig = new EnvironmentConfig();
        envConfig.setTransactional(true);
        envConfig.setAllowCreate(true);
        this.envDir = envDir;
        System.out.println(this.envDir);
        env = new Environment(new File(this.envDir), envConfig);
        open(dbName);
        
	}
	
	void open(String dbName){
		txn = env.beginTransaction(null, null);
        DatabaseConfig dbConfig = new DatabaseConfig();
        dbConfig.setAllowCreate(true);
        dbConfig.setTransactional(true);
//        dbConfig.setSortedDuplicates(true);
        this.db = env.openDatabase(txn,dbName,dbConfig);
        catalog =  new StoredClassCatalog( env.openDatabase(null, "ClassCatalogDB", dbConfig) );
	}	
	
	void close(){
		if(txn != null){
			txn.commit();
		}
		if (db != null) {
			db.close();
        }
	}
	abstract <T> boolean set(List<T> params);
	abstract <T> List<T> get(String name);
	
}
